package com.hongkzh.www.look.media.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.hongkzh.www.R;
import com.hongkzh.www.look.media.view.adapter.SubListViewAdapter;
import com.hongkzh.www.look.media.view.adapter.TradeRootListViewAdapter;
import com.hongkzh.www.view.activity.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 自媒体--招聘--行业筛选
 * Created by as on 2018/4/13.
 */

public class TradeSearchActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.listView_firmName)
    ListView listViewFirmName;
    @BindView(R.id.listView_occupationName)
    ListView listViewOccupationName;
    @BindView(R.id.layout_firmName)
    FrameLayout layoutFirmName;
    @BindView(R.id.layout_occupation)
    FrameLayout layoutOccupation;
    @BindView(R.id.iv_cancel)
    ImageView ivCancel;

    private int selectedPosition;
    /**
     * 根目录的节点
     */
    private String[] roots = new String[]{"行业名称", "行业名称", "行业名称", "行业名称", "行业名称", "行业名称", "行业名称"};

    /**
     * 子目录节点
     */
    private String[][] sub_items = new String[][]{
            new String[]{"海淀区", "西城区", "石景山区", "东城区", "朝阳区"},
            new String[]{"离我最近", "人气最高", "评价最好", "人均最低", "人均最高"},
            new String[]{"全部类型", "优惠券商户", "闪惠商户", "预约服务", "上门服务"},
            new String[]{"海淀区", "西城区", "石景山区", "东城区", "朝阳区"},
            new String[]{"离我最近", "人气最高", "评价最好", "人均最低", "人均最高"},
            new String[]{"全部类型", "优惠券商户", "闪惠商户", "预约服务", "上门服务"},
            new String[]{"全部行业", "全部行业", "全部行业", "全部行业", "全部行业"}
    };

    private ListView subListView;
    private TradeRootListViewAdapter mRootAdapter;

    private final int RESULTCODE = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trade_search;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        setTranslucent(R.color.color_D8D8D8);
    }

    @Override
    protected void init() {

        layoutFirmName.setVisibility(View.VISIBLE);
        layoutOccupation.setVisibility(View.INVISIBLE);
        mRootAdapter = new TradeRootListViewAdapter(this);
        mRootAdapter.setItems(roots);
        listViewFirmName.setAdapter(mRootAdapter);

        listViewFirmName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

                /**
                 * 选中root某项时改变该ListView item的背景色
                 */
                mRootAdapter.setSelectedPosition(position);
                mRootAdapter.notifyDataSetInvalidated();

                selectedPosition = position;
                SubListViewAdapter subAdapter = new SubListViewAdapter(
                        TradeSearchActivity.this, sub_items, position);
                listViewOccupationName.setAdapter(subAdapter);

                /**
                 * 选中某个根节点时，使显示相应的子目录可见
                 */
                layoutOccupation.setVisibility(View.VISIBLE);
                listViewOccupationName
                        .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            @Override
                            public void onItemClick(
                                    AdapterView<?> parent, View view,
                                    int position, long id) {
                                // TODO Auto-generated method stu
//                                showPopBtn.setText(sub_items[selectedPosition][position]);
                                Toast.makeText(TradeSearchActivity.this, "点击了" + sub_items[selectedPosition][position],
                                        Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent();
                                intent.putExtra("jobName", sub_items[selectedPosition][position]);
                                TradeSearchActivity.this.setResult(RESULT_OK, intent);
                                TradeSearchActivity.this.finish();
                            }
                        });

            }
        });
    }

    @Override
    protected void setListener() {
        ivCancel.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_cancel:
                TradeSearchActivity.this.setResult(1);
                this.finish();
                break;
        }

    }

    @Override
    public void onError(Exception e) {

    }


}
